import java.util.ArrayDeque;
import java.util.Queue;

/**
 * @author LKQ
 * @date 2022/1/29 10:42
 * @description
 */
public class FormalSolution {
    public static void main(String[] args) {

    }
    public int[][] highestPeak(int[][] g) {
        int m = g.length, n = g[0].length;
        // 设置高度，非水域设置未-1，水域设置为0
        int[][] ans = new int[m][n];
        Queue<int[]> d = new ArrayDeque<>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (g[i][j] == 1) {
                    d.add(new int[]{i, j});
                }
                ans[i][j] = g[i][j] == 1 ? 0 : -1;
            }
        }
        int[][] dirs = new int[][]{{1,0}, {-1,0}, {0,1}, {0,-1}};
        // 每一轮的高度
        int h = 1;
        while (!d.isEmpty()) {
            int size = d.size();
            while (size-- > 0) {
                int[] info = d.poll();
                int x = info[0], y = info[1];
                for (int[] di : dirs) {
                    int nx = x + di[0], ny = y + di[1];
                    if (nx >= 0 && nx < m && ny >=0 && ny < n && ans[nx][ny] == -1) {
                        ans[nx][ny] = h;
                        d.add(new int[]{nx, ny});
                    }

                }
            }
            h++;
        }
        return ans;
    }

}
